package com.heer.gymnasium.module.hobby.views;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.SignHelper;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.module.hobby.beans.HobbyItemBean;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.main.beans.CategoryBean;
import com.heer.gymnasium.module.main.views.MainActivity;
import com.heer.gymnasium.module.store.beans.AreaBean;
import com.heer.gymnasium.util.ImageUtil;
import com.heer.imagepick.DensityUtils;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.view.MyGridView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/2/11
 */

public class HobbySetActivity extends BaseActivity {
    private static final int RES_ID_LAYOUT = R.layout.layout_hobby_set;
    private static final int RES_ID_ICON_IV = R.id.layout_hobby_set_icon_iv;
    private static final int RES_ID_TITLE_TV = R.id.layout_hobby_set_title_tv;
    private static final int RES_ID_GV = R.id.layout_hobby_set_gv;
    private static final int RES_ID_COMPLETE = R.id.layout_hobby_set_complete_tv;

    private static final int RES_ID_TIME_LL = R.id.layout_hobby_set_time_ll;
    private static final int RES_ID_TIME_WEEKDAY_AM = R.id.layout_hobby_set_weekday_am_cb;
    private static final int RES_ID_TIME_WEEKDAY_PM = R.id.layout_hobby_set_weekday_pm_cb;
    private static final int RES_ID_TIME_WEEKDAY_NIGHT = R.id.layout_hobby_set_weekday_night_cb;
    private static final int RES_ID_TIME_HOLIDAY_AM = R.id.layout_hobby_set_holiday_am_cb;
    private static final int RES_ID_TIME_HOLIDAY_PM = R.id.layout_hobby_set_holiday_pm_cb;
    private static final int RES_ID_TIME_HOLIDAY_NIGHT = R.id.layout_hobby_set_holiday_night_cb;

    private static final int RES_ID_ITEM = R.layout.item_hobby;
    private static final int RES_ID_ITEM_LL = R.id.item_hobby_ll;
    private static final int RES_ID_ITEM_IV = R.id.item_hobby_iv;
    private static final int RES_ID_ITEM_TV = R.id.item_hobby_tv;

    private List<HobbyItemBean> data;
    private List<CategoryBean> categoryBeanList;

    QuickTypeAdapter<HobbyItemBean> adapter;
    private Activity $this;
    private String hobby_time;


    private int from = 0;

    @BindView(RES_ID_GV)
    MyGridView gridView;
    @BindView(RES_ID_TIME_LL)
    LinearLayout timeView;
    @BindView(RES_ID_ICON_IV)
    ImageView imageView;
    @BindView(RES_ID_TITLE_TV)
    TextView lableTextView;

    @BindView(RES_ID_TIME_WEEKDAY_AM)
    CheckBox weekdaysAmCheckBox;
    @BindView(RES_ID_TIME_WEEKDAY_PM)
    CheckBox weekdaysPmCheckBox;
    @BindView(RES_ID_TIME_WEEKDAY_NIGHT)
    CheckBox weekdaysNightCheckBox;
    @BindView(RES_ID_TIME_HOLIDAY_AM)
    CheckBox holidayAmCheckBox;
    @BindView(RES_ID_TIME_HOLIDAY_PM)
    CheckBox holidayPmCheckBox;
    @BindView(RES_ID_TIME_HOLIDAY_NIGHT)
    CheckBox holidayNightCheckBox;

    public static final int requestcode = 0x10;

    public static void startActivity(Activity context, int hobbyType, String param) {
        Intent intent = new Intent(context, HobbySetActivity.class);
        intent.putExtra(EXTRA_FROM, hobbyType);
        intent.putExtra(EXTRA_DATA, param);
        context.startActivityForResult(intent, requestcode);
    }

    @Override
    protected void init() {
        super.init();
        setContentView(RES_ID_LAYOUT);
        $this = this;
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("拼单爱好设置");
        titleUtils.startMainActivity(this);
        from = getIntent().getIntExtra(EXTRA_FROM, 0);
        data = new ArrayList<>();
        categoryBeanList = new ArrayList<>();
        adapter = new QuickTypeAdapter<HobbyItemBean>(this, data, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, HobbyItemBean data, int type, int pos) {
                fillData(holder, data);
            }

            @Override
            public void bindListener4View(ViewHolder holder, final HobbyItemBean data, int type, int pos) {
                super.bindListener4View(holder, data, type, pos);
                holder.getParentView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (data.isSelectStatus()) {
                            data.setSelectStatus(false);
                        } else {
                            data.setSelectStatus(true);
                        }
                        notifyDataSetChanged();
                    }
                });
            }
        };
        gridView.setAdapter(adapter);
        switch (from) {
            case HobbyItemBean.HobbyType.category:
                imageView.setBackgroundResource(R.drawable.ic_notice);
                lableTextView.setText("爱好活动");
                getCategoryList();
                break;
            case HobbyItemBean.HobbyType.area:
                imageView.setBackgroundResource(R.drawable.ic_hobby_location);
                lableTextView.setText("活动区域");
                getAreaList();
                break;
            case HobbyItemBean.HobbyType.time:
                imageView.setBackgroundResource(R.drawable.ic_hobby_time);
                lableTextView.setText("活动时间");
                gridView.setVisibility(View.GONE);
                timeView.setVisibility(View.VISIBLE);
                String times = getIntent().getStringExtra(EXTRA_DATA);
                if (!TextUtils.isEmpty(times) && !times.contains("-1")) {
                    if (times.contains("1")) {
                        weekdaysAmCheckBox.setChecked(true);
                    } else {
                        weekdaysAmCheckBox.setChecked(false);
                    }
                    if (times.contains("2")) {
                        weekdaysPmCheckBox.setChecked(true);
                    } else {
                        weekdaysPmCheckBox.setChecked(false);
                    }
                    if (times.contains("3")) {
                        weekdaysNightCheckBox.setChecked(true);
                    } else {
                        weekdaysNightCheckBox.setChecked(false);
                    }
                    if (times.contains("4")) {
                        holidayAmCheckBox.setChecked(true);
                    } else {
                        holidayAmCheckBox.setChecked(false);
                    }
                    if (times.contains("5")) {
                        holidayPmCheckBox.setChecked(true);
                    } else {
                        holidayPmCheckBox.setChecked(false);
                    }
                    if (times.contains("6")) {
                        holidayNightCheckBox.setChecked(true);
                    } else {
                        holidayNightCheckBox.setChecked(false);
                    }
                }
                break;
            default:
                break;
        }
    }

    @OnClick(RES_ID_TIME_WEEKDAY_AM)
    public void onClickForWeekdayAm(View view) {

    }

    @OnClick(RES_ID_TIME_WEEKDAY_PM)
    public void onClickForWeekdayPM(View view) {

    }

    @OnClick(RES_ID_COMPLETE)
    public void onClickForComplete(View view) {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_TYPE, from);
        if (from == HobbyItemBean.HobbyType.area || from == HobbyItemBean.HobbyType.category) {
            intent.putExtra(EXTRA_DATA, (Serializable) data);
        } else {
            StringBuffer buffer = new StringBuffer();
            if (weekdaysAmCheckBox.isChecked()) {
                buffer.append("1" + ";");
            }
            if (weekdaysPmCheckBox.isChecked()) {
                buffer.append("2" + ";");
            }
            if (weekdaysNightCheckBox.isChecked()) {
                buffer.append("3" + ";");
            }
            if (holidayAmCheckBox.isChecked()) {
                buffer.append("4" + ";");
            }
            if (holidayPmCheckBox.isChecked()) {
                buffer.append("5" + ";");
            }
            if (holidayNightCheckBox.isChecked()) {
                buffer.append("6" + ";");
            }
            if (!TextUtils.isEmpty(buffer.toString())) {
                hobby_time = buffer.toString().substring(0, buffer.toString().length() - 1);
            }
            if (TextUtils.isEmpty(hobby_time)) {
                hobby_time = "-1";
            }
            intent.putExtra(EXTRA_DATA, hobby_time);
        }
        setResult(RESULT_OK, intent);
        finish();
    }

    //     item数据填充
    private void fillData(ViewHolder holder, HobbyItemBean data) {
        if (data.isSelectStatus()) {
            holder.setBg(RES_ID_ITEM_LL, R.drawable.yellow_button_bg);
        } else {
            holder.setBg(RES_ID_ITEM_LL, R.drawable.yellow_border);
        }
        holder.setText(RES_ID_ITEM_TV, data.getName());
        switch (from) {
            case HobbyItemBean.HobbyType.category:
                holder.getView(RES_ID_ITEM_LL).setPadding(DensityUtils.dip2px($this, 15), DensityUtils.dip2px($this, 5),
                        DensityUtils.dip2px($this, 15), DensityUtils.dip2px($this, 5));
                holder.setVisibility(RES_ID_ITEM_IV, View.VISIBLE);
                ImageUtil.loadUrl($this, data.getIconUrl(), R.drawable.ic_hobby_glof, (ImageView) holder.getView(RES_ID_ITEM_IV));
                break;
            case HobbyItemBean.HobbyType.area:
                holder.getView(RES_ID_ITEM_LL).setPadding(DensityUtils.dip2px($this, 5), DensityUtils.dip2px($this, 10),
                        DensityUtils.dip2px($this, 5), DensityUtils.dip2px($this, 10));
                holder.setVisibility(RES_ID_ITEM_IV, View.GONE);
                break;
        }
    }

    //获取类型列表
    private void getCategoryList() {
        mAppHelper.apiService().getCategoryList("").enqueue(new BaseCallback<ApiResponse<List<CategoryBean>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<CategoryBean>>> call, Response<ApiResponse<List<CategoryBean>>> response) {
                data.addAll(HobbyItemBean.dataChange(response.body().getData(), HobbyItemBean.HobbyType.category));
                if (!TextUtils.isEmpty(getIntent().getStringExtra(EXTRA_DATA))) {
                    String[] ids = getIntent().getStringExtra(EXTRA_DATA).split(";");
                    if (ids != null) {
                        for (String s : ids) {
                            for (int i = 0; i < data.size(); i++) {
                                if (data.get(i).getId().equals(s)) {
                                    data.get(i).setSelectStatus(true);
                                    break;
                                }
                            }
                        }
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    //获取区域列表
    private void getAreaList() {
        SignHelper signHelper = new SignHelper("");
        mAppHelper.apiService().getAreaList(signHelper.GetParameter("key")).enqueue(new BaseCallback<ApiResponse<List<AreaBean>>>() {
            @Override
            public void onSuccess(Call<ApiResponse<List<AreaBean>>> call, Response<ApiResponse<List<AreaBean>>> response) {
                data.addAll(HobbyItemBean.dataChange(response.body().getData(), HobbyItemBean.HobbyType.area));
                if (!TextUtils.isEmpty(getIntent().getStringExtra(EXTRA_DATA))) {
                    String[] ids = getIntent().getStringExtra(EXTRA_DATA).split(";");
                    if (ids != null) {
                        for (String s : ids) {
                            for (int i = 0; i < data.size(); i++) {
                                if (data.get(i).getId().equals(s)) {
                                    data.get(i).setSelectStatus(true);
                                    break;
                                }
                            }
                        }
                    }
                }
                adapter.notifyDataSetChanged();
            }

        });
    }
}
